package com.xiaowei.gym.gymjsp.ware.dao.impl;

import com.mysql.cj.util.StringUtils;
import com.xiaowei.gym.gymjsp.ware.dao.EquipmentDao;
import com.xiaowei.gym.gymjsp.ware.entity.AdminEntity;
import com.xiaowei.gym.gymjsp.ware.entity.EquipmentEntity;
import com.xiaowei.gym.gymjsp.ware.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EquipmentDaoImpl implements EquipmentDao {
    @Override
    public int getCount() {
        String sql = "SELECT count(1) FROM gym_equipment";
        Integer integer = 0;
         try {
           integer = JdbcUtil.executeQuery(sql, new JdbcUtil.ResultSetHandler<Integer>() {
               @Override
               public Integer handle(ResultSet rs) throws SQLException {
                   int counts = 0;
                   while (rs.next()) {
                       counts = rs.getInt("count(1)");
                   }
                   return counts;
               }
           }, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return integer;
    }

    @Override
    public List<EquipmentEntity> getList(String name, int currentPageNo, Integer pageSize) {
        StringBuffer sql = new StringBuffer();
        List<EquipmentEntity> equipmentEntities = new ArrayList<>();
        sql.append("SELECT * FROM gym_equipment ");
        List<Object> list = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(name)){
            sql.append("WHERE name like ?");
            list.add("%"+name+"%");
        }
        //在mysql数据库中，分页使用 limit startIndex，pageSize ; 总数
        sql.append(" order by id DESC limit ?,?");
        currentPageNo = (currentPageNo - 1) * pageSize;
        list.add(currentPageNo);
        list.add(pageSize);
        Object[] params = list.toArray();
        System.out.println("sql ----> " + sql.toString());
        try {
            List<EquipmentEntity> equipmentEntityList = new ArrayList<>();
            equipmentEntities = JdbcUtil.executeQuery(sql.toString(), rs -> {
                while (rs.next()) {
                    EquipmentEntity equipmentEntity = new EquipmentEntity();
                    equipmentEntity.setId(rs.getInt("id"));
                    equipmentEntity.setName(rs.getString("name"));
                    equipmentEntity.setStatus(rs.getString("status"));
                    equipmentEntity.setDescription(rs.getString("description"));
                    equipmentEntityList.add(equipmentEntity);
                }
                return equipmentEntityList;
            }, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return equipmentEntities;
    }

    @Override
    public EquipmentEntity getById(Integer valueOf) {
        String sql = "SELECT * FROM gym_equipment WHERE id = ?";
        EquipmentEntity equipmentEntity = null;
        try {
            equipmentEntity = JdbcUtil.executeQuery(sql, new JdbcUtil.ResultSetHandler<EquipmentEntity>() {
                @Override
                public EquipmentEntity handle(ResultSet rs) throws SQLException {
                    EquipmentEntity equipmentEntity = new EquipmentEntity();
                    if (rs.next()) {
                        equipmentEntity.setId(rs.getInt("id"));
                        equipmentEntity.setName(rs.getString("name"));
                        equipmentEntity.setStatus(rs.getString("status"));
                        equipmentEntity.setDescription(rs.getString("description"));
                    }
                    return equipmentEntity;
                }
            }, valueOf);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return equipmentEntity;
    }

    @Override
    public int updateEquipment(EquipmentEntity equipmentEntity) {
        String sql = "UPDATE gym_equipment SET name = ?, status = ?,description = ? WHERE id = ?";
        Object[] params = {equipmentEntity.getName(),equipmentEntity.getStatus(),
                equipmentEntity.getDescription(),equipmentEntity.getId()};
        int i = 0;
        try {
            i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int createEquipment(EquipmentEntity equipmentEntity) {
        String sql = "INSERT INTO gym_equipment(name,status,description) values(?,?,?)";
        Object[] params = {equipmentEntity.getName(),equipmentEntity.getStatus(),equipmentEntity.getDescription()};
        int i = 0;
        try {
           i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int[] deleteEquipmentByIds(Integer[] delId) {
        String sql = "DELETE FROM gym_equipment WHERE id = ?";
        Object[] params = delId;
        System.out.println(params);
        int i[] = null;
        try {
            i = JdbcUtil.batchDelete(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
}
